{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 文本生成\n",
    "\n",
    "## 说明\n",
    "本文展示了如何使用appbuilder内置组件以及如何快速构建全新的文本生成组件。以下是一个在营销场景下生成各种文本内容的样例。\n",
    "\n",
    "## 概览\n",
    "具体地，本样例按照以下流程进行文本生成：\n",
    "1. 使用**空模板**构建一个全新的文本生成组件：商品信息生成组件。\n",
    "2. 输入一个商品，使用新构建的商品信息生成组件生成该商品的商品信息。\n",
    "3. 基于生成的商品信息，使用内置的**问答对挖掘**组件生成问答对。生成的问答对可用于客服等。\n",
    "4. 基于生成的商品信息，使用内置的**风格写作**组件为该商品生成一份【小红书】文案。\n",
    "5. 使用内置的**标签抽取**组件对生成的文案进行标签抽取。抽取出的标签可用于检索文案。\n",
    "\n",
    "以下我们会演示如何实现我们自己的定制化能力。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 流程演示\n",
    "### Step 0: 安装Python SDK\n",
    "我们的appbuilder支持使用pip安装（要求Python >= 3.8）："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [],
   "source": [
    "pip install appbuilder-sdk"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "代码中需要配置用户的`APPBUILDER_TOKEN`。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "# 设置环境变量\n",
    "os.environ['APPBUILDER_TOKEN'] = '...'"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "安装成功后，我们就可以搭建我们自己的能力了。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 1: 商品信息生成\n",
    "基于appbuilder提供的**空模板**，用户可以自定义文本生成组件以解决多样化的需求。这里我们构建一个**商品信息生成**组件以生成商品信息："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from appbuilder import Message, Playground\n",
    "\n",
    "# 输入到大模型中的prompt的模板\n",
    "prompt_template = \\\n",
    "'''输入商品名，我需要你为我生成该商品的商品信息。\n",
    "\n",
    "要求：\n",
    "- 你生成的商品信息应该包含多方面信息。\n",
    "\n",
    "商品名：{product_name}\n",
    "商品信息：\n",
    "'''\n",
    "# 创建商品信息生成组件\n",
    "product_information_generation = Playground(prompt_template=prompt_template, model='ernie-bot-4')\n",
    "\n",
    "# 获取商品信息\n",
    "# 填充prompt_template参数的参数映射表，需要与prompt_template对应\n",
    "prompt_template_kwargs = {\n",
    "    'product_name': '特斯拉Model Y'\n",
    "}\n",
    "response = product_information_generation(Message(prompt_template_kwargs), stream=False, temperature=0.5)\n",
    "product_information = response.content\n",
    "print(f'商品信息：\\n{product_information}')"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输出结果：\n",
    "\n",
    "```\n",
    "商品信息：\n",
    "特斯拉Model Y是一款全电动SUV，具有出色的性能、安全和智能科技特点。以下是该商品的详细信息：\n",
    "\n",
    "1. 车型概述：特斯拉Model Y是一款中型豪华SUV，结合了特斯拉的电动车技术和现代化设计。它提供了宽敞的内部空间、强大的动力和令人印象深刻的续航里程。\n",
    "2. 外观设计：特斯拉Model Y的外观设计简洁而现代，具有流线型车身和独特的LED大灯组。车顶的全景玻璃天窗为乘客提供了极佳的视野。\n",
    "3. 内饰设计：内饰方面，特斯拉Model Y采用了极简的设计风格，中控台配备了一个大尺寸触摸屏，用于控制车辆的各种功能。座椅舒适，后排座椅可按比例放倒，增加储物空间。\n",
    "4. 动力系统：特斯拉Model Y提供了多种动力选择，包括后轮驱动和全轮驱动配置。其电动机可产生惊人的扭矩，加速迅猛。根据配置不同，最大续航里程可达到数百公里。\n",
    "5. 充电和续航：特斯拉Model Y支持特斯拉超级充电站和家庭充电桩进行充电。在特斯拉超级充电站，车辆可以在短时间内快速充满电。\n",
    "6. 智能科技：特斯拉Model Y配备了特斯拉自家的Autopilot自动驾驶辅助系统，可以实现自动驾驶、自动泊车、车道保持等功能。此外，车辆还支持OTA在线升级，不断获得新功能和改进。\n",
    "7. 安全性能：特斯拉Model Y在安全性能方面表现出色，配备了多项主动安全技术，如碰撞预警、自动紧急制动、盲点监测等。同时，车身结构坚固，为乘客提供良好的保护。\n",
    "8. 价格和购买方式：特斯拉Model Y的价格根据车型和配置的不同而异。您可以通过特斯拉官方网站或授权经销商进行购买。\n",
    "```"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 2: 问答对生成\n",
    "基于Step 1生成的商品信息我们生成一些问答对，这里我们使用内置的**问答对生成**组件。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from appbuilder import QAPairMining\n",
    "\n",
    "# 初始化问答对生成组件\n",
    "qa_pair_mining = QAPairMining(model='eb-turbo-appbuilder')\n",
    "\n",
    "# 获取问答对\n",
    "response = qa_pair_mining(Message(product_information), stream=False, temperature=1e-10)\n",
    "qa_pairs = response.content\n",
    "print(f'问答对：\\n{qa_pairs}')"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输出结果：\n",
    "\n",
    "```\n",
    "问答对：\n",
    "问题：特斯拉Model Y是一款什么车型？\n",
    "答案：特斯拉Model Y是一款全电动SUV。\n",
    "\n",
    "问题：特斯拉Model Y的外观设计如何？\n",
    "答案：特斯拉Model Y的外观设计简洁而现代，具有流线型车身和独特的LED大灯组。车顶的全景玻璃天窗为乘客提供了极佳的视野。\n",
    "\n",
    "问题：特斯拉Model Y的内饰设计风格是什么？\n",
    "答案：内饰方面，特斯拉Model Y采用了极简的设计风格，中控台配备了一个大尺寸触摸屏，用于控制车辆的各种功能。座椅舒适，后排座椅可按比例放倒，增加储物空间。\n",
    "\n",
    "问题：特斯拉Model Y提供了哪些动力选择？\n",
    "答案：特斯拉Model Y提供了多种动力选择，包括后轮驱动和全轮驱动配置。其电动机可产生惊人的扭矩，加速迅猛。根据配置不同，最大续航里程可达到数百公里。\n",
    "\n",
    "问题：特斯拉Model Y的充电方式有哪些？\n",
    "答案：特斯拉Model Y支持特斯拉超级充电站和家庭充电桩进行充电。在特斯拉超级充电站，车辆可以在短时间内快速充满电。\n",
    "\n",
    "问题：特斯拉Model Y配备了哪些智能科技？\n",
    "答案：特斯拉Model Y配备了特斯拉自家的Autopilot自动驾驶辅助系统，可以实现自动驾驶、自动泊车、车道保持等功能。此外，车辆还支持OTA在线升级，不断获得新功能和改进。\n",
    "\n",
    "问题：特斯拉Model Y在安全性能方面有哪些特点？\n",
    "答案：特斯拉Model Y在安全性能方面表现出色，配备了多项主动安全技术，如碰撞预警、自动紧急制动、盲点监测等。同时，车身结构坚固，为乘客提供良好的保护。\n",
    "```"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 3: 营销文案生成\n",
    "基于Step 1生成的商品信息生成【小红书】文案，这里我们使用内置的**风格写作**组件。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from appbuilder import StyleWriting\n",
    "\n",
    "# 构造query\n",
    "query = \\\n",
    "f'''请你基于以下商品信息生成文案：\n",
    "{product_information}\n",
    "'''\n",
    "\n",
    "# 初始化风格写作组件\n",
    "style_writing = StyleWriting(model='ernie-bot-4')\n",
    "\n",
    "# 获取小红书文案\n",
    "response = style_writing(Message(query), style_query='小红书', length=300)\n",
    "copywriting = response.content\n",
    "print(f'文案：\\n{copywriting}')"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输出结果：\n",
    "\n",
    "```\n",
    "文案：\n",
    "【特斯拉Model Y：电动之美，智能之魅】\n",
    "\n",
    "🚗 探寻未来驾驶的乐趣，特斯拉Model Y带你领略电动之美！流线型车身、独特LED大灯组🔦，展现现代豪华SUV的风采。而车内极简设计、大尺寸触摸屏🖥️，尽显科技感。\n",
    "\n",
    "💥 动力强劲，后驱或全驱配置任君选择，扭矩惊人，加速迅猛！续航里程更是达到数百公里，让你畅享无忧出行🔋。\n",
    "\n",
    "🔌 充电便捷，特斯拉超级充电站和家庭充电桩让你随时随地为爱车补给能量。短时间内快速充满电，不再为充电烦恼💡。\n",
    "\n",
    "🎉 智能科技满满！Autopilot自动驾驶辅助系统实现自动驾驶、自动泊车等功能，让驾驶更轻松。OTA在线升级让你随时拥有最新功能和改进✨。\n",
    "\n",
    "🛡️ 安全性能卓越，多项主动安全技术为你保驾护航。碰撞预警、自动紧急制动等让你驾驶更安心。坚固车身结构为乘客提供良好保护👨‍👩‍👧‍👦。\n",
    "\n",
    "特斯拉Model Y，不仅是一款电动车，更是智能科技的杰作！快来体验吧！#特斯拉ModelY #电动车 #智能科技 #豪华SUV #未来驾驶\n",
    "```"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 4: 标签抽取\n",
    "基于Step 3生成的文案进行标签抽取，这里我们使用内置的**标签抽取**组件。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from appbuilder import TagExtraction\n",
    "\n",
    "# 初始化标签抽取组件\n",
    "tag_extraction = TagExtraction(model='eb-turbo-appbuilder')\n",
    "\n",
    "# 获取标签\n",
    "response = tag_extraction(Message(copywriting), stream=False, temperature=1e-10)\n",
    "tags = response.content\n",
    "print(f'标签：\\n{tags}')"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输出结果：\n",
    "\n",
    "```\n",
    "1.特斯拉ModelY\n",
    "2.电动之美\n",
    "3.智能之魅\n",
    "4.动力强劲\n",
    "5.续航里程\n",
    "6.充电便捷\n",
    "7.自动驾驶\n",
    "8.OTA在线升级\n",
    "9.安全性能\n",
    "10.保护乘客\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "bf58028eab835c8b5fcb0ca40b311fc21e6cba5ccf47f1b42d31ca2545cde380"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
